package teleradiologia.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.Authentication;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.ui.TargetUrlResolver;
import org.springframework.security.ui.savedrequest.SavedRequest;

public class LoginRedirectTargetUrlResolver implements TargetUrlResolver {

	@Override
	public String determineTargetUrl(SavedRequest savedRequest,
			HttpServletRequest reqest, Authentication authentication) {

		String result = "/";

		GrantedAuthority[] authorities = authentication.getAuthorities();

		for(GrantedAuthority authority : authorities) {
			if(authority.getAuthority().equals("ROLE_ADMINISTRATOR")) {
				result = "/admin/adminHome.htm";
			} else if(authority.getAuthority().equals("ROLE_DOCTOR")) {
				result = "/doctor/doctorHome.htm";
			} else if(authority.getAuthority().equals("ROLE_TECHNICIAN")) {
				result = "/technician/technicianHome.htm";
			} else if(authority.getAuthority().equals("ROLE_PATIENT")) {
				result = "/patient/patientHome.htm";
			} else if(authority.getAuthority().equals("ROLE_RADIOLOGY_MANAGER")) {
				result = "/rad_manager/managerHome.htm";
			} else if(authority.getAuthority().equals("ROLE_DIAGNOSTIC_MANAGER")) {
				result = "/diag_manager/managerHome.htm";
			}
		}

		return result;
	}

}
